/*
	Copyright (C) 2013 Pontificia Universidad Catolica del Peru
	contact: http://avatar.inf.pucp.edu.pe/realidad-aumentada/
	address contact: Av. Universitaria 1801, San Miguel, Lima-Peru.
	
	This file is part of UbicAR PUCP.

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with This program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
	Authors: Alvaro Roger Agüero Sánchez, Rodolfo Javier Galvez Meza
	Pontificia Universidad Catolica del Peru
	Departamento de Ingeniería
*/

package org.grupoavatar.guiaubicua;

import java.util.List;
import java.util.Vector;

import org.grupoavatar.guiaubicua.auxiliares.Item;
import org.grupoavatar.guiaubicua.auxiliares.ListaServicioAdapter;
import org.grupoavatar.guiaubicua.auxiliares.ListaServicioRow;
import org.grupoavatar.guiaubicua.mix.DataView;
import org.grupoavatar.guiaubicua.mix.Marker;
import org.grupoavatar.guiaubicua.mix.MixContext;
import org.grupoavatar.guiaubicua.mix.MixState;
import org.grupoavatar.guiaubicua.mix.data.Servicios;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class VServicios extends SherlockActivity {

	private Vector<String> serviceMenu;
	private Vector<String> serviceDescription;
	private Vector<Boolean> serviceChecked;
	private MixContext mixContext;
	private DataView dataView;
	
	private ListaServicioAdapter adapter;

	private ListView listF;
	public static List<Marker> searchResultMarkers;
	public static List<Marker> originalMarkerList;
	
	public static List<Item> servicios;

	public Vector<String> getServiceMenu() {
		return serviceMenu;
	}

	public Vector<String> getServiceDescription() {
		return serviceDescription;
	}

	public Vector<Boolean> getServiceChecked() {
		return serviceChecked;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
		dataView = VInicial.dataView;
		mixContext = dataView.getContext();
		
		//siempre se recargaran los resultados
		DataView.actualizarMarkers = true;
		dataView.changeState(MixState.NOT_STARTED);
		
		this.setContentView(R.layout.servicio);
		
		//header
//		Button btnLogo = (Button) findViewById(R.id.btnLogo);
//		btnLogo.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				DataView.volverInicio = true;
//				finish();
//			}
//		});
		
		getSherlock().getActionBar().setTitle(R.string.filtro_servicios);
		
		listF = (ListView) findViewById(R.id.listaServicios);

		serviceChecked = new Vector<Boolean>();

		ListaServicioRow listacat_data[] = new ListaServicioRow[Servicios.getCantidadTotalServicios()];

		Servicios.DATASOURCE source;
		for (int i = 1; i <= Servicios.getCantidadTotalServicios(); i++) {
			source = Servicios.DATASOURCE.values()[i];
			serviceChecked.add(mixContext.isServiceSelected(source));
			listacat_data[i-1] = new ListaServicioRow(MixContext.getServiceIcon(source),
												MixContext.getServiceName(source),
												mixContext.isServiceSelected(source));
		}

		adapter = new ListaServicioAdapter(this,
				R.layout.filtro_row, listacat_data);

		listF.setAdapter(adapter);

		listF.setVerticalFadingEdgeEnabled(true); // Make sure that fading
													// is displayed on
													// samsung devices

		listF.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> l, View v, int position,
					long id) {
				
				CheckBox cb = (CheckBox) v.findViewById(R.id.cbxListFiltro);
				cb.toggle();
			}
		});
		
		/*
		listF.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> l, View v,
					int position, long id) {
				
				for (int i = 0; i < serviceChecked.size(); i++){
					if (i == position)	{
						if (serviceChecked.get(i) == false)	{
							CheckBox cb = (CheckBox) v.findViewById(R.id.cbxListFiltro);
							cb.toggle();
						}						
					}
					else	{
						if (serviceChecked.get(i) == true)	{
							//cambiar a not checked
							serviceChecked.set(i, false);
							clickOnService(i);
						}
					}
				}
				
				//volver
				finish();
				return true;
			}
		});
		*/
	}

	public void clickOnService(int position) {
		if (dataView.isFrozen())
			dataView.setFrozen(false);
		mixContext.toogleService(servicios.get(position).servicio);
	}
	
	public void clickOnService(int position, boolean value) {
		if (dataView.isFrozen())
			dataView.setFrozen(false);
		mixContext.toogleService(servicios.get(position).servicio, value);
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
//		int base = Menu.FIRST;
		/*define the first*/
//		menu.add(base, base, base, getString(R.string.menu_item_seleccionar_todos)); //seleccionar todos
//		menu.add(base, base+1, base+1,  getString(R.string.menu_item_deseleccionar_todos)); //deseleccionar todos

		/*assign icons to the menu items*/
		//item1.setIcon(R.drawable.ic_menu_filtrar);
		//item2.setIcon(android.R.drawable.ic_menu_share);
	
		MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.menu_filtros, menu);
		
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
			case R.id.menuSeleccionarTodo:
				activarTodosLosServicios();
				adapter.notifyDataSetChanged();
				break;
			case R.id.menuDeseleccionarTodo:
				desactivarTodosLosServicios();
				adapter.notifyDataSetChanged();
				break;
		}
		return true;
	}
	
	public void activarTodosLosServicios() {
		for (int i = 0; i < servicios.size(); i++) {
			clickOnService(i, true);
			getServiceChecked().set(i, true);
		}
		//Servicios.atLeastOneServiceSelected = true;
	}
	
	public void desactivarTodosLosServicios() {
		for (int i = 0; i < servicios.size(); i++) {
			clickOnService(i, false);
			getServiceChecked().set(i, false);
		}
		//Servicios.atLeastOneServiceSelected = false;
	}
}

